package com.fosung.frame.modules.sys.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.fosung.frame.common.mapper.JsonMapper;
import com.fosung.frame.common.utils.CacheUtils;
import com.fosung.frame.common.utils.SpringContextHolder;
import com.fosung.frame.modules.sys.dao.ParamDao;
import com.fosung.frame.modules.sys.entity.Param;
import com.google.common.collect.Lists;

/**
 * 系统参数工具类
 * 
 * @author fosung
 * @version 2017-02-16
 */
public class ParamUtils {

	public static final String CACHE_PARAM_LIST = "paramList";

	private static ParamDao paramDao = SpringContextHolder.getBean(ParamDao.class);

	public static String getParamValue(String paramCode) {
		if (!StringUtils.isNotBlank(paramCode)) {
			return "";
		}
		for (Param param : getParamList()) {
			if (!paramCode.equals(param.getParamCode())) {
				continue;
			}
			return param.getParamValue();
		}
		return "";
	}

	public static List<Param> getParamList() {
		@SuppressWarnings("unchecked")
		List<Param> paramList = (List<Param>) CacheUtils.get(CACHE_PARAM_LIST);
		if (paramList == null || paramList.size() == 0) {
			paramList = Lists.newArrayList();
			for (Param param : paramDao.findAllList(new Param())) {
				paramList.add(param);
			}
			CacheUtils.put(CACHE_PARAM_LIST, paramList);
		}
		return paramList;
	}

	public static String getParamListJson() {
		return JsonMapper.toJsonString(getParamList());
	}
}
